package org.lmind.example.uc.test;

import java.util.Date;
import java.util.HashMap;

import org.lmind.example.uc.api.UserCenter;
import org.lmind.example.uc.api.UserCenterException;
import org.lmind.example.uc.api.UserCommonAttrubtes;
import org.lmind.example.uc.api.UserInfo;

public class UserCenterTestSuite {
	
	private UserCenter userCenter;
	
	private String userName = "test01";
	
	private String password = "123456";
	
	private Date birthday = new Date();
	
	public UserCenterTestSuite(UserCenter userCenter) {
		super();
		this.userCenter = userCenter;
	}

	public void test() {
		register();
		loginfail();
		login();
		modify();
	}
	
	public void register() {
		
		UserInfo userInfo = userCenter.register(userName, password);
		assert userName.equals(userInfo.getUserName());
		assert userInfo.validatePassword(password);
	}
	
	public void loginfail() {
		try {
			userCenter.login("wrong", password);
			assert false;
		} catch (UserCenterException e) {}
		try {
			userCenter.login(userName, "wrong");
			assert false;
		} catch (UserCenterException e) {}
	}
	
	public void login() {
		
		UserInfo userInfo = userCenter.login(userName, password);
		assert userName.equals(userInfo.getUserName());
	}
	
	public void changePassword() {
		String newPassword = "qwerty";
		userCenter.changePassword(userName, password, newPassword);
		userCenter.login(userName, newPassword);
		userCenter.changePassword(userName, newPassword, password);
		userCenter.login(userName, password);
	}
	
	public void modify() {
		
		HashMap<String, Object> attributes = new HashMap<String, Object>();
		attributes.put(UserCommonAttrubtes.SEX, true);
		attributes.put(UserCommonAttrubtes.BIRTHDAY, birthday);
		userCenter.modifyUserInfo(userName, password, attributes);
		
		
		
		UserInfo userInfo = userCenter.login(userName, password);
		assert userInfo.validatePassword(password);
		assert !userInfo.validatePassword("wrong password");
		assert (Boolean)userInfo.getAttributes().getValue(UserCommonAttrubtes.SEX) == true;
		assert birthday.equals(userInfo.getAttributes().getValue(UserCommonAttrubtes.BIRTHDAY));
		
		userCenter.modifyUserInfo(userName, password, null);
	}
}
